/**   
* @Title: LoginController.java 
* @Package com.peter.ssmdemo.controller 
* @Description: TODO(用一句话描述该文件做什么) 
* @author PeterSo  supei_slm@qq.com
* @date 2016年5月17日 上午10:32:21 
* @version V1.0   
*/
package com.peter.ssmdemo.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.peter.ssmdemo.util.CodeImage;
import com.peter.ssmdemo.util.RandomCode;
import com.peter.ssmdemo.util.RandomCode.SecurityCodeLevel;



/** 
* @ClassName: LoginController 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author PeterSo  supei_slm@qq.com
* @date 2016年5月17日 上午10:32:21 
*/
@Controller
public class LoginController {
	
	static Logger logger = LogManager.getLogger(LoginController.class);

	/**
	 * 请求验证码图片并保存到session
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/captcha.jpg")
	public void getCode(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		char[] a = RandomCode.getSecurityCode(4, SecurityCodeLevel.Medium, true);
		String codeString = new String(a);
		// System.out.println("+++++++++++++>code----: "+codeString);
		// System.out.println("+++++++++++++>session----: "+session.getId());
		session.setAttribute(CodeImage.CaptchaCode, codeString);
		try {
			CodeImage.getImageCode(a, 120, 40, 30, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		logger.info("login into ...");
		return "user/login";
	}
	

	/**
	 * Shiro验证错误，指定错误信息返回页面
	 * @param username
	 * @param password
	 * @param captcha
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String username,
			@RequestParam(FormAuthenticationFilter.DEFAULT_PASSWORD_PARAM) String password,
			@RequestParam String captcha, Model model, HttpServletRequest request) {
		String code = (String) request.getSession().getAttribute(CodeImage.CaptchaCode);
		try {
			if (!code.equals(captcha)) {
				model.addAttribute("message", "验证码错误。");
			}else{
				/*
				User u = userService.checkAccount(username);
				if (u != null) {
					model.addAttribute("message", "用户密码错误。");
				} else {
					model.addAttribute("message", "用户不存在。");
				}
				*/
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			model.addAttribute("message", "登录异常,请联系管理员。");
		}
		return "user/login";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout() {
		SecurityUtils.getSubject().logout();
		return "../../index";
	}

}
